package com.gj.subclass.service.impl;

import com.gj.subclass.enumpackge.StatusEnum;
import com.gj.subclass.enumpackge.StatusEnum2;
import com.gj.subclass.mapper.SubClassMapper;
import com.gj.subclass.model.*;
import com.gj.subclass.service.SubClassService;
import com.gj.utli.AllRecords;
import com.gj.utli.ResultBuilderVo;
import com.gj.utli.ResultVo;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Tangyunhen on 2021-06-24.
 */
@Service
public class SubClassServiceImpl implements SubClassService {
    @Autowired
    private SubClassMapper subClassMapper;

    @Override
    public ResultVo queryPage(SubClassSelectVo subClassSelectVo) {
          //查询数据
        List<SubClassVo> subClassVoList=subClassMapper.queryData(subClassSelectVo);
      for (SubClassVo subClassVo:subClassVoList){
         /* if(subClassVo.getStatus()==1){
              subClassVo.setStatusName("启用");
          }else if (subClassVo.getStatus()==0){
              subClassVo.setStatusName("禁用");
          }*/
              Boolean status = subClassVo.getStatus();
              Integer status2=status==true?1:0;
          subClassVo.setStatusName(StatusEnum2.getStatusName(status2));
      }

        //查询数量
        int count=subClassMapper.queryCount(subClassSelectVo);
        AllRecords allRecords=new AllRecords();
        allRecords.setPageIndex(subClassSelectVo.getPageIndex());
        allRecords.setPageSize(subClassSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.setDataList(subClassVoList);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    @Transactional
    public ResultVo add(SubClassAddVo subClassAddVo) {
        //新增时不能重复
        int count=subClassMapper.queryByName(subClassAddVo.getName());
        if (count==count&&count!=0){
            return ResultBuilderVo.error("名称已经存在了");
        }
         SubClassVo subClassVo1=subClassMapper.queryByCode(subClassAddVo.getCode());
       if (subClassVo1==subClassVo1 &&subClassVo1!=null){
           return ResultBuilderVo.error("编码已经存在");
       }
       SubClassVo subClassVo=new SubClassVo();
       BeanUtils.copyProperties(subClassAddVo,subClassVo);//把左边参数赋值给右边
        Date nowDate =new Date();
        subClassVo.setCreateTime(nowDate);
        subClassVo.setCreateName("项羽");
        subClassVo.setUpdateTime(nowDate);
        subClassVo.setUpdateName("项羽");
        subClassMapper.insert(subClassAddVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid SubClassUpdateVo subClassUpdateVo) {
        //禁用状态才能更改  验证禁用
        SubClassVo subClass= subClassMapper.queryById(subClassUpdateVo.getId());
        if (subClass==null){
            return  ResultBuilderVo.error("修改对象不存在");
        }
        if (subClass.getStatus()){
            return ResultBuilderVo.error("禁用状态下才能修改");
        }


        int count=subClassMapper.queryByName(subClassUpdateVo.getName());

        if (count!=0){
            return ResultBuilderVo.error("名称已经存在了");
        }

        SubClassVo subClassVo=new SubClassVo();
        BeanUtils.copyProperties(subClassUpdateVo,subClassVo);//把左边参数赋值给右边
        Date nowDate =new Date();
        subClassVo.setUpdateTime(nowDate);
        subClassVo.setUpdateName("项羽");
        subClassMapper.update(subClassUpdateVo);
        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo updatestatus(@Valid SubClassUpdateStatusVo subClassUpdateStatusVo) {
        SubClassVo subClass= subClassMapper.queryById(subClassUpdateStatusVo.getId());
        if (subClass==null){
            return  ResultBuilderVo.error("修改对象不存在");
        }
        SubClassVo subClassVo=new SubClassVo();
        BeanUtils.copyProperties(subClassUpdateStatusVo,subClassVo);//把左边参数赋值给右边
        Date nowDate =new Date();
        subClassVo.setUpdateTime(nowDate);
        subClassVo.setUpdateName("项羽");
        subClassMapper.updateStatus(subClassVo);
        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo see(Integer id) {
       SubClassVo subClassVo=subClassMapper.queryById(id);
       if (subClassVo==null){
           return ResultBuilderVo.error("修改对象不存在");
       }
        Boolean status = subClassVo.getStatus();
        Integer status2=status==true?1:0;
        subClassVo.setStatusName(StatusEnum2.getStatusName(status2));
        return ResultBuilderVo.success(subClassVo);
    }
}
